<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <script src="js/jquery-3.3.1.js"></script>
    <script>




        $(function () {
            $("#form").submit(function () {
                if (checkNameAuthor()&&checkDate()) {
                $.ajax({
                    url: "book/addBook",
                    Type: "post",
                    data: $("#form").serialize(),
                    success: function (data) {
                     window.location.href="login.html";
                    },
                    error:function (data) {
                        window.location.href="login.html";
                    }
                })
                return false;
            }
            })
            $("#b_author").blur(checkNameAuthor);
            $("#b_time").blur(checkDate);
        })
        function checkNameAuthor() {
            var name = $("#b_name").val();
            var author = $("#b_author").val();
            if(name.length>0||author.length>0){
                return true;
            }else {

                alert(confirm("用户名和作者不能为空"));
                return false;
            }
        }
        function checkDate() {
           var time = $("#b_time").val();

           var reg=/^\d{4}[-]\d{2}[-]\d{2}$/;

           if(reg.test(time)){
               return true;
           }else {

               alert(confirm("你输入的格式不对"));
               return false;
           }

       }
    </script>
</head>
<body>
<form id="form">
    <table align="center" border="2px">
        <tr ><th align="center" colspan="3"><h4>新增图书信息</h4></th></tr>
        <tr>
       <td>图书名字:</td><td><input type="text" name="name" id="b_name"></td>
        </tr>
        <tr>
       <td> 图书作者:</td><td><input type="text" name="author" id="b_author"></td>
        </tr>
        <tr>
        <td> 购买日期:</td><td><input type="text" name="time" id="b_time"></td><td>yyyy-MM-dd格式</td>
        </tr>
        <tr>
         <td>图书类别:</td><td>
        <select name="type">
        <option value="0">计算机软件</option>
        <option value="1">小说文摘</option>
         <option value="2">杂项</option>
        </select></td>
        </tr>
        <tr><td colspan="3" align="center"><input type="submit" value="提交" id="btn_add"></td></tr>
    </table>
</form>
</body>
</html>